import pymysql
from config_manager import config

def verify_industry_data():
    # 连接数据库
    try:
        conn = pymysql.connect(
            host=config['database']['host'],
            port=config['database']['port'],
            user=config['database']['user'],
            password=config['database']['password'],
            database=config['database']['database'],
            cursorclass=pymysql.cursors.DictCursor
        )
        cursor = conn.cursor()
    except Exception as e:
        print(f"数据库连接失败: {e}")
        return
    
    # 查询各行业的股票数量
    print("\n=== 各行业股票数量统计 ===")
    cursor.execute("SELECT industry, COUNT(*) as count FROM stock_info GROUP BY industry ORDER BY count DESC")
    industries = cursor.fetchall()
    for row in industries:
        print(f"{row['industry']}: {row['count']} 条")
    
    # 从每个行业中选择几个示例股票
    print("\n=== 各行业示例股票 ===")
    for row in industries:
        cursor.execute(
            "SELECT symbol, stock_name, symbol_standardized, industry FROM stock_info "
            "WHERE industry = %s LIMIT 3", 
            (row['industry'],)
        )
        examples = cursor.fetchall()
        print(f"\n{row['industry']} 示例:")
        for example in examples:
            print(f"  {example['symbol']} - {example['stock_name']} (标准化代码: {example['symbol_standardized']})")
    
    # 检查是否还有null或'其他'值
    cursor.execute(
        "SELECT COUNT(*) FROM stock_info WHERE industry IS NULL OR industry = %s OR industry = %s",
        ('其他', '其他行业')
    )
    remaining_other = cursor.fetchone()['COUNT(*)']
    print(f"\n=== 验证结果 ===")
    print(f"剩余未分类股票: {remaining_other} 条")
    
    # 检查总股票数
    cursor.execute("SELECT COUNT(*) FROM stock_info")
    total_stocks = cursor.fetchone()['COUNT(*)']
    print(f"总股票数: {total_stocks} 条")
    
    conn.close()

if __name__ == "__main__":
    verify_industry_data()